<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2021/12/6
 * Time: 11:30
 */

//判断数组是否为空  为空则退出 不继续执行
if (count($_FILES)==0){
    exit("上传文件为空");
}
if (!isset($_COOKIE["Token"])){
    exit("请先登录！");
}

$fileName="";
//获取数组长度
$len=count($_FILES["pic"]["name"]);
for ($i=0;$i<$len;$i++){
    $tmp_name=$_FILES["pic"]["tmp_name"][$i];
    //切割原文件名获取原文件后缀名 base.jpg=>arr[ [0]->base,[1]->jpg]
    $arr=explode(".",$_FILES["pic"]["name"][$i]);
    $postfix=$arr[count($arr)-1];
    //随机0-99999999 拼接获取到原文件后缀名
    $fileName=rand(0,99999999).".".$postfix;
    move_uploaded_file($tmp_name,$fileName);
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>上传成功! 您的头像：</h1>
<input type="hidden" id="url" value="<?php echo $fileName ?>">
<img id="imgBase"  width="200px" height="200px" >
<img id="img1"  width="100px" height="100px">
<img id="img2" width="50px" height="50px">
</body>
<script>
    var imgURL=document.getElementById("url").value;
    document.getElementById('imgBase').src=imgURL;
    document.getElementById('img1').src=imgURL;
    document.getElementById('img2').src=imgURL;
</script>
</html>
